package com.wujunshen.tdd.flags;

import com.wujunshen.tdd.common.FlagType;
import com.wujunshen.tdd.model.Parameter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.wujunshen.tdd.common.FlagType.INTEGER;

/**
 * @author frank woo(吴峻申) <br>
 *     email:<a href="mailto:frank_wjs@hotmail.com">frank_wjs@hotmail.com</a> <br>
 * @date 2021/12/15 14:01<br>
 */
public class IntegerFlag implements Flag<Integer> {
  public static final List<Parameter<Integer>> INTEGER_PARAMETERS = new ArrayList<>();

  @Override
  public void initParameters(String flag, FlagType type) {
    if (type == INTEGER) {
      INTEGER_PARAMETERS.add(new Parameter<>(flag, INTEGER, 0));
    }
  }

  @Override
  public void setValue(String flag, Iterator<String> argIter) {
    for (Parameter<Integer> parameter : INTEGER_PARAMETERS) {
      if (flag.equals(parameter.getFlag())) {
        parameter.setValue(Integer.valueOf(argIter.next().trim()));
      }
    }
  }

  @Override
  public Integer getValue(String flag) {
    for (Parameter<Integer> parameter : INTEGER_PARAMETERS) {
      if (flag.equals(parameter.getFlag())) {
        return parameter.getValue();
      }
    }
    throw new IllegalArgumentException("flag type mismatch: " + flag);
  }
}
